package com.spring.mvc.dao;

import com.spring.mvc.pojo.Department;
import com.spring.mvc.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Admin on 9/23 0023.
 */
@Repository
public class EmployeeDao {
    private static Map<Integer,Employee> employeeMap=new HashMap<Integer, Employee>();

    @Autowired
    private DepartmentDao departmentDao;

    static {
        employeeMap = new HashMap<Integer, Employee>();
        employeeMap.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
        employeeMap.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));
        employeeMap.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));
        employeeMap.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-EE")));
        employeeMap.put(1005, new Employee(1005, "E-FF", "ee@163.com", 1, new Department(105, "D-FF")));

    }

    private  static Integer initId=1006;

    public void save(Employee employee){
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
        employeeMap.put(employee.getId(),employee);
    }

    public Collection<Employee> getAll(){
        return employeeMap.values();
    }

    public Employee getEmployee(Integer id){
        return employeeMap.get(id);
    }

    public void delete(Integer id){
        employeeMap.remove(id);
    }

}
